package org.chen.processor;

import lombok.extern.slf4j.Slf4j;
import org.chen.domain.Article;
import org.chen.domain.CrawlerTask;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import java.time.LocalDateTime;
import us.codecraft.webmagic.selector.Selectable;
import java.util.List;

@Slf4j
public class SocceroddPageProcessor implements PageProcessor {
    private final Site site = Site.me()
            .setRetryTimes(3)
            .setSleepTime(1000)
            .setTimeOut(10000)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");

    private final CrawlerTask task;

    public SocceroddPageProcessor(CrawlerTask task) {
        this.task = task;
    }

    @Override
    public void process(Page page) {
        log.info("Processing page: {}", page.getUrl().toString());

        // 如果是列表页面
        if (page.getUrl().toString().equals(task.getStartUrl())) {
            // 获取所有文章链接
            List<String> links = page.getHtml()
                    .css("div.postList a", "href")  // 使用CSS选择器
                    .all();

            log.info("Found {} article links", links.size());

            // 添加文章链接到爬取队列
            for (String link : links) {
                if (!link.startsWith("http")) {
                    link = "https://www.soccerodd.com" + link;
                }
                page.addTargetRequest(link);
            }

            // 列表页面不需要解析内容
            page.setSkip(true);

        } else {
            // 文章详情页面
            try {
                // 提取标题
                String title = page.getHtml()
                        .css("h1.page-title", "text")
                        .get();

                log.info("Extracted title: {}", title);

                // 提取内容
                Selectable contentNode = page.getHtml().css("div.post-description");
                String content = contentNode.css("p", "text").get();

                log.info("Extracted content: {}", content);

                if (title != null && !title.isEmpty()) {
                    Article article = new Article();
                    article.setTitle(title);
                    article.setDescription(content);
                    article.setUrl(page.getUrl().toString());
                    article.setSource("soccerodd");
                    article.setCategory("soccerodd");
                    article.setCreateTime(LocalDateTime.now());

                    // 设置分类信息
                    article.setCategoryId(task.getCategoryId());         // 从任务中获取分类ID
                    article.setCategoryPath(task.getCategoryPath());     // 从任务中获取分类路径

                    // 将文章对象存入Page
                    page.putField("article", article);
                    log.info("Article created: {}", article.getTitle());
                } else {
                    log.warn("No title found for page: {}", page.getUrl());
                    page.setSkip(true);
                }
            } catch (Exception e) {
                log.error("Error processing article page: {}", e.getMessage(), e);
                page.setSkip(true);
            }
        }
    }

    @Override
    public Site getSite() {
        return site;
    }
}